<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<title>{{@$datainfo->title}}</title>
<meta name="keywords" content="{{@$datainfo->title}}">
<meta name="description" content="{{@$datainfo->title}}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="{{asset('plugins/awardRotate/awardRotate.css')}}">

<script type="text/javascript" src="{{asset('common/js/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{asset('plugins/awardRotate/awardRotate.js')}}"></script>
<script type="text/javascript" src="{{asset('plugins/layer/layer.min.js')}}"></script>

</head>
<style>
.Iluck_box {
    width: 100vw;
    height: 56vw;
    max-width: 640px;
    max-height: 362px;
    background: url({{asset('home/award/luck.png')}}) no-repeat center;
    background-size: 100% 100%;
    margin: 0 auto;
}
.relative {
    position: relative;
}
.turnplate{font-size:16px!important;}
</style>
<body style="background:#261155;overflow-x:hidden;">
@if(count($datainfo))
<!--luck start-->
<div class="Iluck_box relative"></div>
<!--luck end-->

<div class="banner" style="margin-top:-60px;padding:15px;">
	<div class="turnplate" style="background-image:url({{asset('home/award/turnplate-bg.png')}});background-size:100% 100%;padding-top:3px;">
		<canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
		@if(@$datainfo->status)
			<img class="pointer" id="topointer" src="{{asset('home/award/turnplate-pointer.png')}}"/>
		@else
			<img class="pointer" style="opacity:.6;" src="{{asset('home/award/turnplate-end.png')}}"/>
		@endif
	</div>
</div>

<!-- 大转盘图片集合 -->
@if(@$datainfo->pic)
	@foreach(json_decode($datainfo->pic,true) as $k => $pic)
		<img src="{{asset($pic)}}" id="picjson_{{$k}}" style="display:none">
	@endforeach
@endif
<!-- 大转盘图片集合 -->

<script type="text/javascript">
var restr = [];
var turnplate={
	restaraunts:[],				//大转盘奖品名称
	colors:[],					//大转盘奖品区块对应背景颜色
	outsideRadius:192,			//大转盘外圆的半径
	textRadius:155,				//大转盘奖品位置距离圆心的距离
	insideRadius:68,			//大转盘内圆的半径
	startAngle:0,				//开始角度
	bRotate:false				//false:停止;ture:旋转
};

$(document).ready(function(){
	
	//动态添加大转盘的奖品与奖品区域背景颜色
	turnplate.colors = ["#FFF4D6", "#FFFFFF"];
	turnplate.restaraunts = {!!@$datainfo->name!!};
	
	
	//旋转转盘 item:奖品位置; txt：提示语;
	var rotateFn = function (item){
		var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2));
		if(angles<270){
			angles = 270 - angles; 
		}else{
			angles = 360 - angles + 270;
		}

		$('#wheelcanvas').rotate({
			angle:0,
			animateTo:angles+1800, //转3圈
			duration:10000, // 旋转速度
			callback:function (){
				layer.msg('恭喜你获得:'+restr.award,{time:1200},function(){
					location.reload();
				});
				turnplate.bRotate = !turnplate.bRotate;
			}
		});
	};

	$('#topointer').click(function (){
		if(turnplate.bRotate){
			return;
		}
		$.ajax({
			type : "get",
			url : '/m/award/roll',
			dataType : 'json',
			success : function(str){
				if(str.status > 0){
					turnplate.bRotate = !turnplate.bRotate;
					var item = str.rid;
					restr['award'] = str.award;
					restr['atype'] = str.atype;
					restr['awid'] = str.awid;
					rotateFn(item);
				}else{
					layer.msg(str.info,{time: 2500});
				}
			}
		})
	});
	
});

//页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染
window.onload=function(){
	drawRouletteWheel();
};

function drawRouletteWheel() {    
  var canvas = document.getElementById("wheelcanvas"); 
  if (canvas.getContext) {
	  //根据奖品个数计算圆周角度
	  var arc = Math.PI / (turnplate.restaraunts.length/2);
	  var ctx = canvas.getContext("2d");
	  //在给定矩形内清空一个矩形
	  ctx.clearRect(0,0,422,422);
	  //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式  
	  ctx.strokeStyle = "#FFBE04";
	  //font 属性设置或返回画布上文本内容的当前字体属性
	  ctx.font = '18px 微软雅黑';      
	  for(var i = 0; i < turnplate.restaraunts.length; i++) {       
		  var angle = turnplate.startAngle + i * arc;
		  ctx.fillStyle = turnplate.colors[i%(turnplate.colors.length)];
		  ctx.beginPath();
		  //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线（用于创建圆或部分圆）    
		  ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);    
		  ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
		  ctx.stroke();  
		  ctx.fill();
		  //锁画布(为了保存之前的画布状态)
		  ctx.save();
		  //----绘制奖品开始----
		  ctx.fillStyle = "#E5302F";
		  var text = turnplate.restaraunts[i];
		  var line_height = 17;
		  //translate方法重新映射画布上的 (0,0) 位置
		  ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);
		  //rotate方法旋转当前的绘图
		  ctx.rotate(angle + arc / 2 + Math.PI / 2);
		  ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
		  
		  var img= document.getElementById('picjson_'+i);
		  img.onload=function(){  
			  ctx.drawImage(img,-25,10,50,55);      
		  }; 
		  ctx.drawImage(img,-25,10,50,55);
		  
		  //把当前画布返回（调整）到上一个save()状态之前 
		  ctx.restore();
	  }     
  } 
}
</script>
@endif
</body>
</html>
